package com.wintop.springbootjwt.controller;

import com.wintop.springbootjwt.annotation.PassToken;
import com.wintop.springbootjwt.annotation.UserLoginToken;
import com.wintop.springbootjwt.bean.User;
import com.wintop.springbootjwt.dao.SysUserDao;
import com.wintop.springbootjwt.service.UserService;
import com.wintop.springbootjwt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {


    @Autowired
    UserService userService;

    @Autowired
    SysUserDao sysUserDao;

    @PostMapping ("/login")
    public Result login(String name ,String password){
    User byUsername = sysUserDao.findByUsername(name);
        try {
            if (byUsername==null) {
                throw new Exception("用户不存在!");
            }
            if (!password.equals(byUsername.getPassword())){
                throw new Exception("账号或密码错误!");
            }
            // 生成Token
            String webToken = userService.createWebToken(byUsername);
            Map<String,Object> map =new HashMap<>();
            map.put("code",200);
            map.put("token",webToken);
            return Result.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }

    @UserLoginToken
    @GetMapping("/getMessage")
    public String getMessage(){
        return "你已通过验证";
    }
}
